package com.yungam.imcs.controller.dimission_chain;

import com.yungam.common.bean.dimension.DimensionCase;
import com.yungam.common.bean.dimension.MaterialType;
import com.yungam.common.response.RetDTO;
import com.yungam.common.service.DimensionCaseService;
import com.yungam.common.service.MaterialTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "尺寸链案例页面接口")
@RestController
@RequestMapping("/dimension")
public class DimensionCaseController {

    @Resource
    private DimensionCaseService caseService;
    @Resource
    private MaterialTypeService typeService;

    @ApiOperation("获取所有的尺寸链案例信息")
    @GetMapping("/case")
    public RetDTO<?> getDimensionCases() {
        List<DimensionCase> list = caseService.list();
        // 查询材料类型对应的名称
        List<MaterialType> types = typeService.list(); // 这里查询全部材料, 包括已经删除的材料, 因为材料删除了, 案例并不会删除
        Map<Integer, String> map = new HashMap<>();
        for (MaterialType mt : types) { // 建立映射
            String name = mt.getTrash() ? "DELETED-" + mt.getName() : mt.getName();
            map.put(mt.getId(), name);
        }
        // 组装数据
        for (DimensionCase dc : list) {
            if (map.containsKey(dc.getType())) dc.setTypeName(map.get(dc.getType()));
        }
        return RetDTO.data(list);
    }

    @ApiOperation("删除某一尺寸链案例")
    @DeleteMapping("/case")
    public RetDTO<?> deleteDimensionCase(@ApiParam("id") Integer id) {
        caseService.removeById(id);
        return RetDTO.success();
    }

    @ApiOperation("保存某一尺寸链案列")
    @PostMapping("/case")
    public RetDTO<?> addDimensionCase(@ApiParam("尺寸链案列实体")
                                        @RequestBody DimensionCase dimension) {
        dimension.setCreateTime(new Date());
        caseService.save(dimension);
        return RetDTO.success();
    }

    @ApiOperation("修改某一尺寸链案列")
    @PutMapping("/case")
    public RetDTO<?> updateDimensionCase(@ApiParam("尺寸链案列实体")
                                            @RequestBody DimensionCase dimension) {
        caseService.updateById(dimension);
        return RetDTO.success();
    }

    @ApiOperation("添加罐号")
    @PostMapping("/saveRestFields")
    public RetDTO<?> addRestFields(@ApiParam("尺寸链案列实体")
                                      @RequestBody DimensionCase dimension) {
        DimensionCase dc = new DimensionCase();
        dc.setId(dimension.getId()).setProductSn(dimension.getProductSn());
        caseService.updateById(dc);
        return RetDTO.success();
    }

}
